{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 01PRE.ipynb\n",
    "repeatedly run this notebook by changing the varible \"filename\" \n",
    "- filename = 'dataset/train.tsv'\n",
    "- filename = 'dataset/valid.tsv'\n",
    "- filename = 'dataset/testA.tsv'\n",
    "- filename = 'dataset/testB.tsv'\n",
    "\n",
    "05/27/2020"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = 'dataset/testB.tsv'\n",
    "import pandas as pd\n",
    "df = pd.read_csv(filename, quoting=3, sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29005\n",
      "29005\n"
     ]
    }
   ],
   "source": [
    "# remove queries like \"'s xxx\" as reported in the forum\n",
    "print(len(df))\n",
    "df = df[~df['query'].str.startswith('\\'s')]\n",
    "print(len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import base64\n",
    "def decode(row):\n",
    "    row['features'] = np.frombuffer(base64.b64decode(row['features']), \n",
    "                                 dtype=np.float32).reshape(row['num_boxes'], 2048).astype(np.float16)\n",
    "    row['boxes'] = np.frombuffer(base64.b64decode(row['boxes']), \n",
    "                                 dtype=np.float32).reshape(row['num_boxes'], 4).astype(np.float16)\n",
    "    row['class_labels'] = np.frombuffer(base64.b64decode(row['class_labels']), \n",
    "                                 dtype=np.int64).reshape(row['num_boxes'])\n",
    "    return row\n",
    "df = df.apply(decode, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tokenizer(vocabulary_size=28996, model=BertWordPiece, add_special_tokens=True, unk_token=[UNK], sep_token=[SEP], cls_token=[CLS], clean_text=True, handle_chinese_chars=True, strip_accents=True, lowercase=False, wordpieces_prefix=##)\n"
     ]
    }
   ],
   "source": [
    "from tokenizers import BertWordPieceTokenizer\n",
    "tokenizer = BertWordPieceTokenizer(\"dataset/bert-base-cased-vocab.txt\", lowercase=False)\n",
    "print(tokenizer)\n",
    "def query2tokenid(query):\n",
    "    return tokenizer.encode(query).ids\n",
    "df['query_tokenid'] = df['query'].apply(query2tokenid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_pickle(filename.replace('tsv', 'pkl').replace('dataset', 'extracted'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# using 10% of train for faster development\n",
    "# df.sample(300000).to_pickle('extracted/train_tiny.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>product_id</th>\n",
       "      <th>image_h</th>\n",
       "      <th>image_w</th>\n",
       "      <th>num_boxes</th>\n",
       "      <th>boxes</th>\n",
       "      <th>features</th>\n",
       "      <th>class_labels</th>\n",
       "      <th>query</th>\n",
       "      <th>query_id</th>\n",
       "      <th>query_tokenid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>103042011</td>\n",
       "      <td>800</td>\n",
       "      <td>800</td>\n",
       "      <td>9</td>\n",
       "      <td>[[111.0, 428.0, 274.0, 541.0], [174.0, 125.0, ...</td>\n",
       "      <td>[[0.0, 0.02663, 0.2598, 0.0, 0.2322, 0.0, 0.0,...</td>\n",
       "      <td>[28, 28, 28, 28, 28, 9, 28, 28, 28]</td>\n",
       "      <td>wooden file cabinet</td>\n",
       "      <td>0</td>\n",
       "      <td>[101, 4122, 4956, 6109, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>101177314</td>\n",
       "      <td>750</td>\n",
       "      <td>750</td>\n",
       "      <td>4</td>\n",
       "      <td>[[245.0, 330.0, 335.0, 449.0], [5.0, 198.0, 73...</td>\n",
       "      <td>[[0.0, 0.0, 6.926, 0.0, 0.437, 0.0, 0.0, 0.0, ...</td>\n",
       "      <td>[28, 9, 28, 28]</td>\n",
       "      <td>wooden file cabinet</td>\n",
       "      <td>0</td>\n",
       "      <td>[101, 4122, 4956, 6109, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>103009949</td>\n",
       "      <td>800</td>\n",
       "      <td>800</td>\n",
       "      <td>6</td>\n",
       "      <td>[[138.0, 522.0, 709.0, 782.0], [405.0, 428.0, ...</td>\n",
       "      <td>[[0.0, 0.0, 0.0138, 0.0, 0.011536, 0.0, 0.0, 0...</td>\n",
       "      <td>[28, 28, 28, 28, 28, 28]</td>\n",
       "      <td>wooden file cabinet</td>\n",
       "      <td>0</td>\n",
       "      <td>[101, 4122, 4956, 6109, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103022608</td>\n",
       "      <td>800</td>\n",
       "      <td>800</td>\n",
       "      <td>7</td>\n",
       "      <td>[[423.0, 460.0, 506.0, 533.0], [123.0, 149.0, ...</td>\n",
       "      <td>[[0.0, 0.1203, 0.9106, 0.0, 0.6167, 0.0, 0.0, ...</td>\n",
       "      <td>[28, 28, 28, 28, 9, 9, 9]</td>\n",
       "      <td>wooden file cabinet</td>\n",
       "      <td>0</td>\n",
       "      <td>[101, 4122, 4956, 6109, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>103041034</td>\n",
       "      <td>800</td>\n",
       "      <td>800</td>\n",
       "      <td>5</td>\n",
       "      <td>[[133.0, 144.0, 347.0, 534.0], [134.0, 92.0, 6...</td>\n",
       "      <td>[[0.0, 0.05518, 0.000805, 0.0, 0.06665, 0.0, 0...</td>\n",
       "      <td>[28, 9, 28, 28, 28]</td>\n",
       "      <td>wooden file cabinet</td>\n",
       "      <td>0</td>\n",
       "      <td>[101, 4122, 4956, 6109, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29000</th>\n",
       "      <td>103032444</td>\n",
       "      <td>1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>10</td>\n",
       "      <td>[[179.0, 52.0, 680.0, 432.0], [183.0, 559.0, 6...</td>\n",
       "      <td>[[0.0, 0.0, 1.07, 0.0, 0.003616, 0.0, 0.0, 0.0...</td>\n",
       "      <td>[0, 0, 29, 29, 2, 2, 4, 4, 4, 4]</td>\n",
       "      <td>hedging loose sweater</td>\n",
       "      <td>999</td>\n",
       "      <td>[101, 1119, 13556, 5768, 15195, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29001</th>\n",
       "      <td>103018483</td>\n",
       "      <td>1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>2</td>\n",
       "      <td>[[192.0, 246.0, 365.0, 350.0], [295.0, 168.0, ...</td>\n",
       "      <td>[[0.0, 0.0, 1.14, 0.0, 0.0, 0.287, 0.0, 0.0, 0...</td>\n",
       "      <td>[29, 0]</td>\n",
       "      <td>hedging loose sweater</td>\n",
       "      <td>999</td>\n",
       "      <td>[101, 1119, 13556, 5768, 15195, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29002</th>\n",
       "      <td>103011147</td>\n",
       "      <td>800</td>\n",
       "      <td>800</td>\n",
       "      <td>1</td>\n",
       "      <td>[[96.0, 48.0, 694.0, 732.0]]</td>\n",
       "      <td>[[0.0, 0.0, 0.3809, 0.0, 0.0, 0.0, 0.0, 0.0, 0...</td>\n",
       "      <td>[0]</td>\n",
       "      <td>hedging loose sweater</td>\n",
       "      <td>999</td>\n",
       "      <td>[101, 1119, 13556, 5768, 15195, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29003</th>\n",
       "      <td>103031908</td>\n",
       "      <td>800</td>\n",
       "      <td>800</td>\n",
       "      <td>1</td>\n",
       "      <td>[[0.0, 0.0, 664.0, 800.0]]</td>\n",
       "      <td>[[0.0, 0.0, 0.01732, 0.0, 0.0, 0.5107, 0.0, 0....</td>\n",
       "      <td>[0]</td>\n",
       "      <td>hedging loose sweater</td>\n",
       "      <td>999</td>\n",
       "      <td>[101, 1119, 13556, 5768, 15195, 102]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29004</th>\n",
       "      <td>103048277</td>\n",
       "      <td>759</td>\n",
       "      <td>766</td>\n",
       "      <td>1</td>\n",
       "      <td>[[172.0, 83.0, 741.0, 721.0]]</td>\n",
       "      <td>[[0.0, 0.0, 0.001634, 0.0, 0.0, 0.0, 0.0, 0.0,...</td>\n",
       "      <td>[0]</td>\n",
       "      <td>hedging loose sweater</td>\n",
       "      <td>999</td>\n",
       "      <td>[101, 1119, 13556, 5768, 15195, 102]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>29005 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       product_id  image_h  image_w  num_boxes  \\\n",
       "0       103042011      800      800          9   \n",
       "1       101177314      750      750          4   \n",
       "2       103009949      800      800          6   \n",
       "3       103022608      800      800          7   \n",
       "4       103041034      800      800          5   \n",
       "...           ...      ...      ...        ...   \n",
       "29000   103032444     1000     1000         10   \n",
       "29001   103018483     1000     1000          2   \n",
       "29002   103011147      800      800          1   \n",
       "29003   103031908      800      800          1   \n",
       "29004   103048277      759      766          1   \n",
       "\n",
       "                                                   boxes  \\\n",
       "0      [[111.0, 428.0, 274.0, 541.0], [174.0, 125.0, ...   \n",
       "1      [[245.0, 330.0, 335.0, 449.0], [5.0, 198.0, 73...   \n",
       "2      [[138.0, 522.0, 709.0, 782.0], [405.0, 428.0, ...   \n",
       "3      [[423.0, 460.0, 506.0, 533.0], [123.0, 149.0, ...   \n",
       "4      [[133.0, 144.0, 347.0, 534.0], [134.0, 92.0, 6...   \n",
       "...                                                  ...   \n",
       "29000  [[179.0, 52.0, 680.0, 432.0], [183.0, 559.0, 6...   \n",
       "29001  [[192.0, 246.0, 365.0, 350.0], [295.0, 168.0, ...   \n",
       "29002                       [[96.0, 48.0, 694.0, 732.0]]   \n",
       "29003                         [[0.0, 0.0, 664.0, 800.0]]   \n",
       "29004                      [[172.0, 83.0, 741.0, 721.0]]   \n",
       "\n",
       "                                                features  \\\n",
       "0      [[0.0, 0.02663, 0.2598, 0.0, 0.2322, 0.0, 0.0,...   \n",
       "1      [[0.0, 0.0, 6.926, 0.0, 0.437, 0.0, 0.0, 0.0, ...   \n",
       "2      [[0.0, 0.0, 0.0138, 0.0, 0.011536, 0.0, 0.0, 0...   \n",
       "3      [[0.0, 0.1203, 0.9106, 0.0, 0.6167, 0.0, 0.0, ...   \n",
       "4      [[0.0, 0.05518, 0.000805, 0.0, 0.06665, 0.0, 0...   \n",
       "...                                                  ...   \n",
       "29000  [[0.0, 0.0, 1.07, 0.0, 0.003616, 0.0, 0.0, 0.0...   \n",
       "29001  [[0.0, 0.0, 1.14, 0.0, 0.0, 0.287, 0.0, 0.0, 0...   \n",
       "29002  [[0.0, 0.0, 0.3809, 0.0, 0.0, 0.0, 0.0, 0.0, 0...   \n",
       "29003  [[0.0, 0.0, 0.01732, 0.0, 0.0, 0.5107, 0.0, 0....   \n",
       "29004  [[0.0, 0.0, 0.001634, 0.0, 0.0, 0.0, 0.0, 0.0,...   \n",
       "\n",
       "                              class_labels                  query  query_id  \\\n",
       "0      [28, 28, 28, 28, 28, 9, 28, 28, 28]    wooden file cabinet         0   \n",
       "1                          [28, 9, 28, 28]    wooden file cabinet         0   \n",
       "2                 [28, 28, 28, 28, 28, 28]    wooden file cabinet         0   \n",
       "3                [28, 28, 28, 28, 9, 9, 9]    wooden file cabinet         0   \n",
       "4                      [28, 9, 28, 28, 28]    wooden file cabinet         0   \n",
       "...                                    ...                    ...       ...   \n",
       "29000     [0, 0, 29, 29, 2, 2, 4, 4, 4, 4]  hedging loose sweater       999   \n",
       "29001                              [29, 0]  hedging loose sweater       999   \n",
       "29002                                  [0]  hedging loose sweater       999   \n",
       "29003                                  [0]  hedging loose sweater       999   \n",
       "29004                                  [0]  hedging loose sweater       999   \n",
       "\n",
       "                              query_tokenid  \n",
       "0              [101, 4122, 4956, 6109, 102]  \n",
       "1              [101, 4122, 4956, 6109, 102]  \n",
       "2              [101, 4122, 4956, 6109, 102]  \n",
       "3              [101, 4122, 4956, 6109, 102]  \n",
       "4              [101, 4122, 4956, 6109, 102]  \n",
       "...                                     ...  \n",
       "29000  [101, 1119, 13556, 5768, 15195, 102]  \n",
       "29001  [101, 1119, 13556, 5768, 15195, 102]  \n",
       "29002  [101, 1119, 13556, 5768, 15195, 102]  \n",
       "29003  [101, 1119, 13556, 5768, 15195, 102]  \n",
       "29004  [101, 1119, 13556, 5768, 15195, 102]  \n",
       "\n",
       "[29005 rows x 10 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 50G\n",
      "-rw-rw-r-- 1 amax amax 458M 5月  27 22:43 testA.pkl\n",
      "-rw-rw-r-- 1 amax amax 465M 6月  10 22:43 testB.pkl\n",
      "-rw-rw-r-- 1 amax amax  44G 5月  27 22:41 train.pkl\n",
      "-rw-rw-r-- 1 amax amax 4.4G 5月  27 22:42 train_tiny.pkl\n",
      "-rw-rw-r-- 1 amax amax 234M 5月  27 22:42 valid.pkl\n"
     ]
    }
   ],
   "source": [
    "## finally we have\n",
    "!ls -lh extracted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
